package com.huaxindata.im.main.share;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huaxin.common.utils.Util;
import com.huaxindata.im.R;
import com.huaxindata.im.model.entity.ChatMessageBody;
import com.huaxindata.im.model.entity.ChatStationMessageBody;
import com.huaxindata.im.model.entity.ChatSvgMessageBody;
import com.huaxindata.im.util.Log;

/**
 * Created by Administrator on 2017/10/19.
 * 分享界面弹出的对话框fragment
 */

@SuppressLint("ValidFragment")
public class ShareDialogFragement extends DialogFragment {
    private static final String PARAM1 = "param1";
    private static final String PARAM2 = "param2";
    private static final String PARAM3 = "param3";
    private static final String PARAM4 = "param4";
    private ChatMessageBody      mMessageBody;
    private int                  mPosition;
    private String               mNickName;
    private ShareConfirmListener mListener;
    private Context              mContext;
    private TextView             cancelView;
    private TextView             confirmView;
    private TextView             titleView;
    private EditText             extraView;
    private TextView             deviceName;
    private TextView deviceStatus;
    private TextView deviceAddress;
    private ImageView deviceImage;
    private int message_type;

    public ShareDialogFragement(Context context) {
        this.mContext = context;
        this.mListener = (ShareConfirmListener) context;
    }

    public static ShareDialogFragement newInstance(ChatMessageBody message, int message_type, int position, String nickName, Context context) {
        ShareDialogFragement shareDialogFragement = new ShareDialogFragement(context);
        Bundle bundle = new Bundle();
        bundle.putParcelable(PARAM1, message);
        bundle.putInt(PARAM2, position);
        bundle.putString(PARAM3, nickName);
        bundle.putInt(PARAM4, message_type);
        shareDialogFragement.setArguments(bundle);
        return shareDialogFragement;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mMessageBody = getArguments().getParcelable(PARAM1);
            mPosition = getArguments().getInt(PARAM2);
            mNickName = getArguments().getString(PARAM3);
            message_type = getArguments().getInt(PARAM4);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ShareConfirmListener) {
            mListener = (ShareConfirmListener) context;
            this.mContext = context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement ShareConfirmListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_share_dialog, container, false);
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        initView(view);
        return view;
    }

    private void initView(View view) {
        cancelView = (TextView) view.findViewById(R.id.share_dialog_send_cancel);
        confirmView = (TextView) view.findViewById(R.id.share_dialog_send_confirm);
        titleView = (TextView) view.findViewById(R.id.share_dialog_title);
        extraView = (EditText) view.findViewById(R.id.share_dialog_send_extra);
        deviceName = (TextView) view.findViewById(R.id.station_device_name);
        deviceStatus = (TextView) view.findViewById(R.id.station_device_status);
        deviceAddress = (TextView) view.findViewById(R.id.station_device_address);
        deviceImage = (ImageView) view.findViewById(R.id.station_divece_image);
        titleView.setText("发送给：" + mNickName);
        if (message_type == ChatMessageBody.Type.STATION) {
            ChatStationMessageBody stationMessageBody = (ChatStationMessageBody) mMessageBody;
            initStaionMessage(stationMessageBody);
        } else if (message_type == ChatMessageBody.Type.SVG) {
            ChatSvgMessageBody chatSvgMessageBody = (ChatSvgMessageBody) mMessageBody;
            initSvgMessage(chatSvgMessageBody);
        }

        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        confirmView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.share(mPosition, extraView.getText().toString().trim());
                }
                dismiss();
            }
        });
    }

    private void initSvgMessage(ChatSvgMessageBody chatSvgMessageBody) {
        deviceName.setText(chatSvgMessageBody.getFileName());
        deviceStatus.setText(chatSvgMessageBody.SvgSize);
        deviceAddress.setVisibility(View.GONE);
        Glide.with(mContext).load(chatSvgMessageBody.thumbRemotePath).into(deviceImage);
    }

    private void initStaionMessage(ChatStationMessageBody stationMessageBody) {
       deviceName.setText(stationMessageBody.name);
        String flagState;
        switch (stationMessageBody.status) {
            case ChatStationMessageBody.ChatStationStatus.ChatStation_Normal:
                flagState = "正常";
                break;
            case ChatStationMessageBody.ChatStationStatus.ChatStation_Timeout:
                flagState = "超时";
                break;
            case ChatStationMessageBody.ChatStationStatus.ChatStation_Error:
                flagState = "故障";
                break;
            case ChatStationMessageBody.ChatStationStatus.ChatStation_Build:
                flagState = "建设中";
                break;
            default:
                flagState = stationMessageBody.status;
                break;
        }
        deviceStatus.setText("状态:" + flagState);

        if (Util.checkNull(stationMessageBody.address)) {
            deviceAddress.setVisibility(View.GONE);
        } else {
            deviceAddress.setVisibility(View.VISIBLE);
            deviceAddress.setText(stationMessageBody.address);
        }

        Glide.with(mContext).load(stationMessageBody.remotePath).into(deviceImage);
    }

    protected interface ShareConfirmListener {
        void share(int position, String extraText);
    }
}
